/**
 * Created by Administrator on 2019/2/28.
 */
$CertusJsonAjax.ajax("expenditure/zdbxlcById", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {

    $$scope.expenditureEntity = res.data;
    $$scope.expenditureStepEntity = {
        "processInstanceId": $CertusJsonAjax.getProInstanceId(),
        "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
    }
    $$scope.processs = res.data.processs;

    $$scope.rmb = upDigit($$scope.expenditureEntity.paymentAmount);
})



$$scope.getsssbbb = function(bb){
    ;
    //starting denied sentback others completed
    if(bb == "starting"){
        return "正在进行中";
    }else if(bb == "denied"){
        return "否决";
    }else if(bb == "sentback"){
        return "被退";
    }else if(bb == "others"){
        return "关闭";
    }else if(bb == "completed"){
         return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
    return "待办";
}
//监听项目名称
$$scope.$watch("expenditureEntity.projName", function (newValue, old, scope) {
    if (old && newValue) {
        if (newValue.indexOf(old) > -1) {
            nowProjName = newValue;
            scope.dataList.projNameSelect = scope.dataList.projNameSelect.filter(isSaveProjName);
            if (scope.dataList.projNameSelect.length) {
                if (scope.dataList.projNameSelect.length == 1) {
                    scope.expenditureEntity.projId = scope.dataList.projNameSelect[0].projId;
                    scope.expenditureEntity.projName = scope.dataList.projNameSelect[0].projName;
                }
            }
            if(!scope.dataList.projNameSelect || scope.dataList.projNameSelect.length == 0){
                scope.expenditureEntity.projId = "";
                scope.expenditureEntity.projName = "";
            }
            return;
        }
    }

    //直接去数据库 查询
    $CertusFormAjax.ajax("relatedProj", {param: newValue}, function (res) {
        scope.dataList.projNameSelect = res.data;
        if (scope.dataList.projNameSelect.length) {
            if (scope.dataList.projNameSelect.length == 1) {
                scope.expenditureEntity.projId = scope.dataList.projNameSelect[0].projId;
                scope.expenditureEntity.projName = scope.dataList.projNameSelect[0].projName;
            }
        }
    })
})



$$scope.dataList = {}

$$scope.dataList.costTypeSelect = [
    {name: "支票", code: "check"},
    {name: "电汇", code: "dh"},
    {name: "转账", code: "zz"},
    {name: "其他", code: "qt"}

]

$$scope.costTypeChange = function(a){
    for(var j in $$scope.dataList.costTypeSelect){
        if($$scope.dataList.costTypeSelect[j].code == a){
            $$scope.expenditureEntity.paymentMethodName = $$scope.dataList.costTypeSelect[j].name;
        }
    }
}

//费用分类
$$scope.dataList.costSelect = [
    {name: "火车/汽车", code: "a"},
    {name: "飞机", code: "b"},
    {name: "交通", code: "c"},
    {name: "住宿", code: "d"},
    {name: "出差补助", code: "e"},
    {name: "市内交通费", code: "f"},
    {name: "办公费", code: "h"},
    {name: "低值易耗品", code: "i"},
    {name: "餐费", code: "j"},
    {name: "通讯费", code: "k"},
    {name: "车辆费", code: "l"},
    {name: "水电费", code: "m"},
    {name: "物管费", code: "o"},
    {name: "房屋租赁费", code: "p"},
    {name: "其他租赁费", code: "q"},
    {name: "办公电脑补贴", code: "r"},
    {name: "对外服务费", code: "s"},
    {name: "对外咨询费", code: "t"},
    {name: "外聘费用", code: "u"},
    {name: "证书补贴", code: "v"},
    {name: "外部培训费", code: "w"},
    {name: "礼品", code: "x"},
    {name: "招待费", code: "y"},
]

$$scope.costChange = function(a){
    for(var i in $$scope.dataList.costSelect){
        if($$scope.dataList.costSelect[i].code == a){
            $$scope.expenditureEntity.expenseClassFicationName = $$scope.dataList.costSelect[i].name;
        }
    }
}

//同意 提交
$$scope.zdbxSubmit = function () {
    $CertusJsonAjax.ajax("expenditure/zdbxlcProjApproval", $$scope.expenditureStepEntity, function (res) {
        if (res.data == "success") {
            findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
            history.back();
            return;
        }
        layer.msg("提交过程中遇到错误");
    })
}

var nowCost;

function isSaveProjCost(element) {
    return element.budgetNo.indexOf(nowCost) > -1;
}

//监听项目名称
$$scope.$watch("expenditureEntity.costCode", function (newValue, old, scope) {
    if (old && newValue) {
        if (newValue.indexOf(old) > -1) {
            nowCost = newValue;
            scope.dataList.costSelect1 = scope.dataList.costSelect1.filter(isSaveProjCost);
            if (scope.dataList.costSelect1.length) {
                if (scope.dataList.costSelect1.length == 1) {
                    scope.expenditureEntity.costId = scope.dataList.costSelect1[0].id;
                    scope.expenditureEntity.costCode = scope.dataList.costSelect1[0].budgetNo;
                    $CertusFormAjax.ajax("relatedCostCodeProj", {param: scope.expenditureEntity.costCode}, function (res) {
                        scope.expenditureEntity.projName = res.data.projName;
                        scope.expenditureEntity.projId = res.data.projId;
                    })
                }
            }
            if(!scope.dataList.costSelect1 || scope.dataList.costSelect1.length == 0){
                scope.expenditureEntity.costCode = "";
                scope.expenditureEntity.costId = "";
                scope.expenditureEntity.projName = "";
                scope.expenditureEntity.projId = null;
            }
            return;
        }
    }

    //直接去数据库 查询
    $CertusFormAjax.ajax("relatedCostCode", {param: newValue}, function (res) {
        scope.dataList.costSelect1 = res.data;
        if (scope.dataList.costSelect1.length) {
            if (scope.dataList.costSelect1.length == 1) {
                scope.expenditureEntity.costId = scope.dataList.costSelect1[0].id;
                scope.expenditureEntity.costCode = scope.dataList.costSelect1[0].budgetNo;
                $CertusFormAjax.ajax("relatedCostCodeProj", {param: scope.expenditureEntity.costCode}, function (res) {
                    scope.expenditureEntity.projName = res.data.projName;
                    scope.expenditureEntity.projId = res.data.projId;
                })
            }
        }
    })
})

$CertusJsonAjax.ajax("queryxjdkProcessTrace",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){
    $$scope.processCCBXTrace = res.data;
})

$("#entTime11").datetimepicker({
    format: 'YYYY-MM-DD',
}).on('dp.change', function (e) {
    var result = new moment(e.date).format('YYYY-MM-DD');
    if(result && !angular.isUndefined(result)){
        $$scope.expenditureStepEntity.remitDate= result;

        $$scope.$apply();
    }
})

//汇款确认提交
$$scope.remittance = function(){

    if(certusValidateNotnull()){

        $CertusJsonAjax.ajax("expenditure/remittance", $$scope.expenditureStepEntity, function (res) {
            if (res.data == "success") {
                layer.msg("提交成功");
                history.back();
                return;
            }
            layer.msg("提交过程中遇到错误");
        })

    }
}

$$scope.zdbxlcProjVeto = function () {
    $CertusJsonAjax.ajax("expenditure/zdbxlcProjVeto", $$scope.expenditureStepEntity, function (res) {
        if (res.data == "success") {
            layer.msg("已退回至起草人");
            // findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
            history.back();
            return;
        }
        layer.msg("提交过程中遇到错误");
    })
}

$$scope.zdbxlcProjDenied = function () {
    $CertusJsonAjax.ajax("expenditure/zdbxlcProjDenied", $$scope.expenditureStepEntity, function (res) {
        if (res.data == "success") {
            findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
            history.back();
            return;
        }
        layer.msg("提交过程中遇到错误");
    })
}


//付款方式
$$scope.dataList.costTypeSelect = [
    {name: "支票", code: "check"},
    {name: "电汇", code: "dh"},
    {name: "转账", code: "zz"},
    {name: "其他", code: "qt"}

]

$$scope.costTypeChange = function(a){
    for(var j in $scope.dataList.costTypeSelect){
        if($scope.dataList.costTypeSelect[j].code == a){
            $scope.expenditureEntity.paymentMethodName = $scope.dataList.costTypeSelect[j].name;
        }
    }
}

//费用分类
$$scope.dataList.costSelect = [
    {name: "火车/汽车", code: "a"},
    {name: "飞机", code: "b"},
    {name: "交通", code: "c"},
    {name: "住宿", code: "d"},
    {name: "出差补助", code: "e"},
    {name: "市内交通费", code: "f"},
    {name: "办公费", code: "h"},
    {name: "低值易耗品", code: "i"},
    {name: "餐费", code: "j"},
    {name: "通讯费", code: "k"},
    {name: "车辆费", code: "l"},
    {name: "水电费", code: "m"},
    {name: "物管费", code: "o"},
    {name: "房屋租赁费", code: "p"},
    {name: "其他租赁费", code: "q"},
    {name: "办公电脑补贴", code: "r"},
    {name: "对外服务费", code: "s"},
    {name: "对外咨询费", code: "t"},
    {name: "外聘费用", code: "u"},
    {name: "证书补贴", code: "v"},
    {name: "外部培训费", code: "w"},
    {name: "礼品", code: "x"},
    {name: "招待费", code: "y"},
]

$$scope.costChange = function(a){
    for(var i in $scope.dataList.costSelect){
        if($scope.dataList.costSelect[i].code == a){
            $scope.expenditureEntity.expenseClassFicationName = $scope.dataList.costSelect[i].name;
        }
    }
}
// RMB大写转换
function upDigit(n) {
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);

    var s = '';

    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);

    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}



//提交流程
$$scope.zdbxlcSubmitss = function(){
    if(certusValidateNotnull()){

        if(!$$scope.expenditureEntity.projId){
            layer.msg("项目未填写");
            return;
        }
        $$scope.expenditureStepEntity = {
            "processInstanceId": $CertusJsonAjax.getProInstanceId(),
            "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
            expenditureEntity:$$scope.expenditureEntity
        };
        $CertusJsonAjax.ajax("expenditure/zdbxlcSubmitSecond",$$scope.expenditureStepEntity,function(res){
            if(res.data == "success"){
                findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                window.history.back();
                return;
            }
            layer.msg("提交遇到错误")
        })
    }
}

$$scope.zdbxlcKJHKApproval = function(){

    layer.confirm('已递交打印的纸质单据？', {
        btn: ['已递交', '未递交'] //按钮
    }, function () {
        $CertusJsonAjax.ajax("expenditure/zdbxlcKJHKApproval", $$scope.expenditureStepEntity, function (res) {
            if (res.data == "success") {
                findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
                layer.msg('转入财务审核', {icon: 1});
                history.back();
                return;
            }
            layer.msg("提交过程中遇到错误");
        })
    })
}
//财务经理 提交
$$scope.zdbxlcCWJLApproval = function(){
    $CertusJsonAjax.ajax("expenditure/zdbxlcCWJLApproval", $$scope.expenditureStepEntity, function (res) {
        if (res.data == "success") {
            findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
            history.back();
            return;
        }
        layer.msg("提交过程中遇到错误");
    })
}